<?php

include "lib/lib.php";

/**
 * 
 * 数据格式

 * bond_code 债券代码
 * bond_name 债券名称
 * code      股票代码
 * name      股票名称
 * plan_total 发行量
 * 
 * sub_date  申购日期
 * sign_date 中签公布日
 * listing_date 上市日期
 * plan_total 发行量
 * 

 * 
 * {"0":{"sub_date":"2020-03-16","bond_code":"128101","bond_name":"联创转债","code":"002036","name":"联创电子","sub_code":"072036","share_code":"082036","sign_date":"2020-03-18","plan_total":"3.000000","issue_total":"0.000000","issue_price":"100.0000","success_rate":"0.00000000","listing_date":"0","expire_date":"2026-03-16","price":"18.820","quota":"0.42080","number":"0","market_id":"35","stock_market_id":"33"}}
 */



$wx = new wx(APP_ID,APP_SECRET);

$data = file_get_contents("http://data.10jqka.com.cn/ipo/kzz/");
if($data===false){
    throw new Exception(405,"数据获取失败");
}
$dataArr = json_decode($data,true);
if($dataArr===false){
    throw new Exception(406,"数据解析失败");
}
if(isset($dataArr['status_code']) && $dataArr['status_code']==0){

    $todaySubArr=[]; //今日申购
    $todaySignArr=[]; //今日中签
    $todayListingArr=[];//今日上市

    $todaySubNameArr=[]; //今日申购
    $todaySignNameArr=[]; //今日中签
    $todayListingNameArr=[];//今日上市

    $it = new myIterator($dataArr['list']); 
    $data_i=50;//只查看前50条数据
    foreach($it as $key => $value) 
    {  
        if($data_i==0){
            break;
        }
        $data_i--;
       
        if($value['sub_date']==date("Y-m-d")){//今日申购
            $todaySubArr[]=$value;
            $todaySubNameArr[]=$value['bond_name'];
        }

        if($value['sign_date']==date("Y-m-d")){//今日公布
            $todaySignArr[]=$value;
            $todaySignNameArr[]=$value['bond_name'];
        }

        if($value['listing_date']==date("Y-m-d")){//今日上市
            $todayListingArr[]=$value;
            $todayListingNameArr[]=$value['bond_name'];
        }
    }
	
	if(count($todaySubArr)==0 && count($todaySignArr)==0 && count($todayListingArr)==0){
		return null;
	}
	
    $postData=[];
    $postData['sub']= count($todaySubArr) ? count($todaySubArr)."支可以申购," :"";
    $postData['sign']= count($todaySignArr) ? count($todaySignArr)."支公布申购结果," :"";
    $postData['listing']= count($todayListingArr) ? count($todayListingArr)."支上市," :"";

    $listStr = date("Y年m月d日")."列表数据";
    if(count($todaySubArr)){
        $listStr .= "\r\n可申购列表：".join(",",$todaySubNameArr);
    }
    if(count($todaySignArr)){
        $listStr .= "\r\n公布结果：".join(",",$todaySignNameArr);
    }
    if(count($todayListingArr)){
        $listStr .= "\r\n上市列表：".join(",",$todayListingNameArr);
    }

    $postData['list']=$listStr;

    $userList = $wx->getFansUserList();
    foreach ($userList as $user){
        $result =  $wx->sendTplMsg(formatMsg($user,TPL_ID,$postData));
    }

}
